<?php
class wms_delivery_process{

    /**
     *
     * 发货单校验完成处理方法
     * @param int $delivery_id 发货单ID
     * @param boolean $auto 是否整单校验
     */
    function verifyDelivery($delivery_id,$auto=false){
        $dly_id = $delivery_id;
        $dlyObj = app::get('wms')->model('delivery');
        $dly_itemObj  = app::get('wms')->model('delivery_items');
        $opObj        = app::get('ome')->model('operation_log');
        //对发货单详情进行校验完成处理
        if ($this->verifyItemsByDeliveryId($dly_id)){
            $res = $dlyObj->db->exec("update sdb_wms_delivery set process_status = (process_status | 2) where delivery_id =".$dly_id);
            if (!$res){
                return false;
            }

            //增加捡货绩效
            foreach(kernel::servicelist('tgkpi.pick') as $o){
                if(method_exists($o,'finish_pick')){
                    $o->finish_pick($dly_id);
                }
            }

            if($auto){
                $msg = '发货单校验完成(免校验)';
            }else{
                $msg = '发货单校验完成';
            }

            if (kernel::single('desktop_user')->get_id()){
                $opObj->write_log('delivery_check@wms', $dly_id, $msg);
            }

            //同步校验状态到oms
            $deliveryInfo = $dlyObj->dump($dly_id,'outer_delivery_bn,branch_id');
            $wms_id = kernel::single('ome_branch')->getWmsIdById($deliveryInfo['branch_id']);
            $data = array(
                'delivery_bn' => $deliveryInfo['outer_delivery_bn'],
            );
            $res = kernel::single('wms_event_trigger_delivery')->doCheck($wms_id, $data, true);

            return true;
        }else {

            if (kernel::single('desktop_user')->get_id()){
                $opObj->write_log('delivery_check@wms', $dly_id, '发货单校验未完成');
            }
            return false;
        }
    }

    /**
     *
     * 校验完成，对发货单对应详情进行更新保存方法
     * @param bigint $dly_id
     * @return boolean
     */
    function verifyItemsByDeliveryId($dly_id){
        $dly_itemObj  = app::get('wms')->model('delivery_items');
        $dlyItemsSLObj    = app::get('wms')->model('delivery_items_storage_life');

        $items = $dly_itemObj->getList('item_id,number,verify,verify_num,use_expire', array('delivery_id'=>$dly_id), 0, -1);
        foreach ($items as $item){
            if($item['use_expire'] == 1){
                $item_expire_arr = $dlyItemsSLObj->getList('*',array('delivery_id'=>$dly_id,'item_id'=>$item['item_id']));
                foreach($item_expire_arr as $item_expire){
                    $data['verify'] = 'true';
                    $data['verify_num'] = $item_expire['number'];

                    if ($dlyItemsSLObj->update($data, array('itemsl_id'=>$item_expire['itemsl_id'])) == false) return false;
                    $data = null;
                }
            }

            $data['verify'] = 'true';
            $data['verify_num'] = $item['number'];

            if ($dly_itemObj->update($data, array('item_id'=>$item['item_id'])) == false) return false;
            $data = null;
        }
        return true;
    }

    /**
     *
     * 校验内容的临时保存方法
     * @param int $dly_id 发货单ID
     */
    function verifyItemsByDeliveryIdFromPost($dly_id)
    {
        $basicMaterialBarcode    = kernel::single('material_basic_material_barcode');#基础物料 ExBOY
        $dly_itemObj  = app::get('wms')->model('delivery_items');
        $dlyItemsSLObj    = app::get('wms')->model('delivery_items_storage_life');

        $items = $dly_itemObj->getList('item_id,number,product_id,verify,verify_num,use_expire', array('delivery_id'=>$dly_id), 0, -1);
        foreach ($items as $item)
        {
            if($item['use_expire'] == 1){
                $item_expire_arr = $dlyItemsSLObj->getList('*',array('delivery_id'=>$dly_id,'item_id'=>$item['item_id']));
                $all_item_num = 0;
                foreach($item_expire_arr as $item_expire){
                    $barcode = $item_expire['expire_bn'];
                    $num = intval($_POST['number_'. $barcode]);

                    $num = $num>$item_expire['number']? $item_expire['number'] : $num;
                    $data['verify'] = 'false';
                    $data['verify_num'] = $num;
                    if ($dlyItemsSLObj->update($data, array('itemsl_id'=>$item_expire['itemsl_id'])) == false) return false;
                    $data = null;
                    $all_item_num += $num;
                    $_POST['number_'. $barcode] -= $num;
                }

                $data['verify'] = 'false';
                $data['verify_num'] = $all_item_num;
                if ($dly_itemObj->update($data, array('item_id'=>$item['item_id'])) == false) return false;
                $data = null;
            }else{
                $barcode    = $basicMaterialBarcode->getBarcodeById($item['product_id']);
                $num = intval($_POST['number_'. $barcode]);
                $num = $num>$item['number']? $item['number'] : $num;
                $data['verify'] = 'false';
                $data['verify_num'] = $num;
                if ($dly_itemObj->update($data, array('item_id'=>$item['item_id'])) == false) return false;
                $data = null;
                $_POST['number_'. $barcode] -= $num;
            }
        }
        return true;
    }

    /**
     *
     * 执行具体的发货处理事务
     * @param int $dly_id 发货单id
     * @param string $type 发货处理的方式batch:批量/group:分组
     */
    function consignDelivery($dly_id, $type='') {
        $deliveryObj = app::get('wms')->model('delivery');

        $delivery_time = time();
        $filter['delivery_id'] = $dly_id;
        $dlydata['status'] = 3;
        $dlydata['process_status'] = 7;
        $dlydata['delivery_time'] = $delivery_time;

        $affect_row = $deliveryObj->update($dlydata, $filter);
        if(!is_numeric($affect_row) || $affect_row <= 0){
            return false;
        }else{
            $deliveryInfo = $deliveryObj->dump($dly_id,'outer_delivery_bn,branch_id,weight,delivery_cost_actual');

            //如果发货成功，处理保质期批次的变化:冻结释放，实际数量扣减，单据转正
            $storageLifeReceiptLib = kernel::single('material_receipt_storagelife');
            $storagelife_data['branch_id'] = $deliveryInfo['branch_id'];
            $storagelife_data['bill_id'] = $dly_id;
            $storagelife_data['bill_type'] = 3;
            $storageLifeReceiptLib->consign($storagelife_data ,$msg);

            $msg = '发货单发货完成';
            if($type == wms_const::__BATCH){
                $msg = '发货单发货(批量)完成';
            }elseif($type == wms_const::__GROUP){
                $msg = '发货单发货(分组)完成';
            }

            //发货成功记录相应日志
            $opObj = app::get('ome')->model('operation_log');
            $opObj->write_log('delivery_process@wms', $dly_id, $msg,'',$opinfo);

            //WMS发货单发货触发通知OMS发货
            $wms_id = kernel::single('ome_branch')->getWmsIdById($deliveryInfo['branch_id']);

            $data = array(
                'delivery_bn' => $deliveryInfo['outer_delivery_bn'],
                'delivery_time' => $delivery_time,
                'weight' => $deliveryInfo['weight'],
                'delivery_cost_actual' => $deliveryInfo['delivery_cost_actual'],
            );
            $res = kernel::single('wms_event_trigger_delivery')->consign($wms_id, $data, true);

            return true;
        }
    }

    /**
     * 打回发货单操作
     *
     * @param array() $dly_ids
     * @param string $memo
     * @return boolean
     */
    function rebackDelivery($dly_ids, $memo){
        if (is_array($dly_ids)){
            $ids = $dly_ids;
        }else {
            $ids[] = $dly_ids;
        }
        $data['memo']    = $memo;
        $data['status']  = 1;
        $data['logi_no'] = null;

        $dlyObj            = app::get('wms')->model('delivery');
        $dlyItemsObj       = app::get('wms')->model('delivery_items');
        $dlyBillObj        = app::get('wms')->model('delivery_bill');
        $branch_productObj = app::get('ome')->model('branch_product');
        $dlyCorpObj        = app::get('ome')->model('dly_corp');

        foreach ($ids as $item)    {
            $deliveryInfo = $dlyObj->dump($item,'status, branch_id, outer_delivery_bn, logi_id');
            if ($deliveryInfo['status'] == 3){
                continue;
            }

            $data['delivery_id'] = $item;

            //撤销所有发货单包裹单
            $billdata = array(
                'status'=> 2,
                'logi_no' => null,
            );
            $billfilter = array('delivery_id'=>$item);
            
            //回收电子面单
            $dlyCorp = $dlyCorpObj->dump($deliveryInfo['logi_id'],'tmpl_type,channel_id');

            if ($dlyCorp['tmpl_type'] == 'electron') {
                $logiBillList = $dlyBillObj->getList('logi_no',$billfilter);
                foreach ((array) $logiBillList as $_logi_bill) {
                    if ($_logi_bill['logi_no']) {
                        kernel::single('logisticsmanager_service_waybill')->recycle_waybill($_logi_bill['logi_no'],$dlyCorp['channel_id'],$item);
                    }
                }
            }

            $dlyBillObj->update($billdata,$billfilter);


            //将发货单状态更新为打回并记录备注
            if ($dlyObj->save($data)){
                $wms_id = kernel::single('ome_branch')->getWmsIdById($deliveryInfo['branch_id']);
                $data = array(
                    'delivery_bn' => $deliveryInfo['outer_delivery_bn'],
                    'memo' => $data['memo'],
                );
                $res = kernel::single('wms_event_trigger_delivery')->reback($wms_id, $data, true);
                if($res['rsp'] == 'succ'){
                    //自建仓储发起发货单撤销，保质期条码预占释放
                    $storageLifeReceiptLib = kernel::single('material_receipt_storagelife');
                    $storagelife_data['branch_id'] = $deliveryInfo['branch_id'];
                    $storagelife_data['bill_id'] = $item;
                    $storagelife_data['bill_type'] = 3;
                    $storageLifeReceiptLib->unfreeze($storagelife_data ,$msg);
                }
            }

            //删除唯一码相关信息
            $this->del_serial_info($item);
            
        }
        return true;
    }

    private function del_serial_info($delivery_id){
        //必须是有效wms的delivery_id
        if($delivery_id && intval($delivery_id)>0){
    
            $serialObj = app::get('ome')->model('product_serial');
            $serialLogObj = app::get('ome')->model('product_serial_log');
            //获取log_ids item_ids
            $rs_log = $serialLogObj->getList("log_id,item_id",array("bill_no"=>$delivery_id));
            //判断是否有相关的唯一码信息
            if(empty($rs_log)){
                return;
            }
            //删除唯一码记录及其日志
            foreach($rs_log as $var_rs_log){
                if($var_rs_log["item_id"] && intval($var_rs_log["item_id"])>0){
                    $serialObj->delete(array("item_id"=>$var_rs_log["item_id"]));
                }
                if($var_rs_log["log_id"] && intval($var_rs_log["log_id"])>0){
                    $serialLogObj->delete(array("log_id"=>$var_rs_log["log_id"]));
                }
            }
    
        }
    
    }
    
}
